﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class LoginPanel : BasePanel<LoginPanel>
{
    public UIInput inpAccount;
    public UIInput inpPassword;
    public UIToggle togRePassword;
    public UIToggle togAutoPassword;
    public UIButton btnRegister;
    public UIButton btnLogin;
    public override void Init()
    {
        //账号
        inpAccount.value = AccountMgr.Instance.LastAccount();
        //记住密码
        togRePassword.value = AccountMgr.Instance.IsRePassword();
        //密码
        if (togRePassword.value)
        {
            inpPassword.value = AccountMgr.Instance.LastPassword();
        }
        //自动登录
        togAutoPassword.value = AccountMgr.Instance.IsAutoLogin();
        if (togAutoPassword.value)
        {
            bool result = AccountMgr.Instance.LoginAccount(inpAccount.value, inpPassword.value, togRePassword.value, togAutoPassword.value);
            if (result)
            {
                LoginSuccess();
            }
        }

        btnRegister.onClick.Add(new EventDelegate(() =>
        {
            Hide();
            RegisterPanel.Instance.Show();
        }));
        btnLogin.onClick.Add(new EventDelegate(() =>
        {
            if (inpAccount.value == "")
            {
                TipPanel.Instance.Show("账号不能为空!");
            }
            if (inpPassword.value == "")
            {
                TipPanel.Instance.Show("密码不能为空!");
            }
            bool result = AccountMgr.Instance.LoginAccount(inpAccount.value, inpPassword.value, togRePassword.value, togAutoPassword.value);
            if (result)
            {
                LoginSuccess();
            }
            else
            {
                TipPanel.Instance.Show("账号或密码不正确!");
            }
        }));
    }

    public void LoginSuccess()
    {
        Hide();
        if (AccountMgr.Instance.lastServerId != 0)
        {
            ServerPanel.Instance.Show();
        }
        else
        {
            ServerListPanel.Instance.Show();
        }
    }

    public void Clear()
    {
        inpAccount.value = "";
        inpPassword.value = "";
        togRePassword.value = false;
        togAutoPassword.value = false;
        AccountMgr.Instance.ClearRecord();
    }
}
